//
//  TopUpView.m
//  DiBiao
//
//  Created by GuoShuang on 2017/11/30.
//  Copyright © 2017年 PY. All rights reserved.
//

#import "TopUpView.h"

@interface TopUpView ()

@property (nonatomic, strong)UILabel * title;
@property (nonatomic ,strong)UIButton * weixinButton;//微信支付
@property (nonatomic ,strong)UIButton * zhifubaoButton;//支付宝支付
@property (nonatomic ,strong)UIButton * ensureButton;//取消
@property (nonatomic, strong)UIButton * weixinCoverBtn;
@property (nonatomic, strong)UIButton * zhifubaoCoverBtn;
@property (nonatomic, strong)UILabel * zhifubaoLab;
@property (nonatomic, strong)UILabel * weixinLab;
@end

@implementation TopUpView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.alpha = 0;
        UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [backBtn addTarget:self action:@selector(hidden) forControlEvents:UIControlEventTouchUpInside];
        backBtn.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.75];
        [self addSubview:backBtn];
        
        UIView *whiteView = [[UIView alloc]init];
        whiteView.backgroundColor = [UIColor whiteColor];
        [backBtn addSubview:whiteView];

        //请选择付款方式
        _title = [[UILabel alloc] init];
        _title.text = @"请选择付款方式";
        _title.textColor = [UIColor blackColor];
        _title.font = DEF_FontSize_14;
        _title.textAlignment = NSTextAlignmentLeft;
        [whiteView addSubview:_title];
        
        _zhifubaoLab = [[UILabel alloc] init];
        _zhifubaoLab.text = @"支付宝";
        _zhifubaoLab.textColor = [UIColor blackColor];
        _zhifubaoLab.font = DEF_FontSize_14;
        _zhifubaoLab.textAlignment = NSTextAlignmentLeft;
        [whiteView addSubview:_zhifubaoLab];
        
        _weixinLab = [[UILabel alloc] init];
        _weixinLab.text = @"微信";
        _weixinLab.textColor = [UIColor blackColor];
        _weixinLab.font = DEF_FontSize_14;
        _weixinLab.textAlignment = NSTextAlignmentLeft;
        [whiteView addSubview:_weixinLab];
        
        //微信
        _weixinButton = [[UIButton alloc] init];
        [_weixinButton setImage:[UIImage imageNamed:@"WeChat"] forState:UIControlStateNormal];
        _weixinButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [whiteView addSubview:_weixinButton];
        
        //支付宝
        _zhifubaoButton = [[UIButton alloc] init];
        [_zhifubaoButton setImage:[UIImage imageNamed:@"AliPay"] forState:UIControlStateNormal];
        _zhifubaoButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [whiteView addSubview:_zhifubaoButton];

        //取消
        _ensureButton = [[UIButton alloc] init];
        _ensureButton.titleLabel.font = DEF_FontSize_14;
        _ensureButton.backgroundColor = getColor(whiteColor);
        _ensureButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_ensureButton setTitle:@"取消" forState:UIControlStateNormal];
        [_ensureButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_ensureButton addTarget:self action:@selector(alipayButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [whiteView addSubview:_ensureButton];
        
        self.weixinCoverBtn = [[UIButton alloc] init];
        self.weixinCoverBtn.layer.borderColor = getColor(@"d1d1d1").CGColor;
        self.weixinCoverBtn.layer.borderWidth = 0.5;
        self.weixinCoverBtn.backgroundColor = [UIColor clearColor];
        [self.weixinCoverBtn addTarget:self action:@selector(walletButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [whiteView addSubview:self.weixinCoverBtn];
        
        self.zhifubaoCoverBtn = [[UIButton alloc] init];
        self.zhifubaoCoverBtn.layer.borderColor = getColor(@"d1d1d1").CGColor;
        self.zhifubaoCoverBtn.layer.borderWidth = 0.5;
        self.zhifubaoCoverBtn.backgroundColor = [UIColor clearColor];
        [self.zhifubaoCoverBtn addTarget:self action:@selector(weChatButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [whiteView addSubview:self.zhifubaoCoverBtn];
        
        __weak typeof (self)weakSelf = self;
        [_title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(weakSelf.ensureButton.mas_height);
            make.bottom.equalTo(weakSelf.zhifubaoButton.mas_top).offset(-10/WIDTH_5S_SCALE);
            make.left.equalTo(weakSelf.mas_left).offset(10/WIDTH_5S_SCALE);
        }];
        
        [_zhifubaoButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10/WIDTH_5S_SCALE);
            make.height.width.mas_equalTo(30/WIDTH_5S_SCALE);
            make.bottom.mas_equalTo(weakSelf.weixinButton.mas_top).offset(-10/WIDTH_5S_SCALE);
        }];
        
        [_zhifubaoLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.zhifubaoButton.mas_right).offset(5/WIDTH_5S_SCALE);
            make.top.bottom.equalTo(weakSelf.zhifubaoButton);
        }];
        
        [_zhifubaoCoverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf);
            make.top.equalTo(weakSelf.zhifubaoButton.mas_top).offset(-5/WIDTH_5S_SCALE);
            make.bottom.equalTo(weakSelf.zhifubaoButton.mas_bottom).offset(5/WIDTH_5S_SCALE);
        }];
        
        [_weixinButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.width.height.equalTo(weakSelf.zhifubaoButton);
            make.bottom.equalTo(weakSelf.ensureButton.mas_top).offset(-10/WIDTH_5S_SCALE);
        }];
        
        [_weixinLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.weixinButton.mas_right).offset(5/WIDTH_5S_SCALE);
            make.top.bottom.equalTo(weakSelf.weixinButton);
        }];
        
        [_weixinCoverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf);
            make.top.equalTo(weakSelf.weixinButton.mas_top).offset(-5/WIDTH_5S_SCALE);
            make.bottom.equalTo(weakSelf.weixinButton.mas_bottom).offset(5/WIDTH_5S_SCALE);
        }];
        
        [_ensureButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.mas_left).offset(-1/WIDTH_5S_SCALE);
            make.right.equalTo(weakSelf.mas_right).offset(1/WIDTH_5S_SCALE);
            make.height.mas_equalTo(40/WIDTH_5S_SCALE);
            make.bottom.equalTo(weakSelf.mas_bottom);
        }];
        
        [whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(weakSelf.mas_bottom);
            make.width.mas_equalTo(SCREEN_WIDTH);
            make.top.equalTo(weakSelf.title.mas_top);
        }];
    }
    return self;
}

#pragma mark - Event
- (void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}

- (void)hidden{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


//微信
-(void)walletButtonAction:(UIButton *)sender{
    if (self.weChatBtnClick) {
        self.weChatBtnClick();
    }
}

//支付宝
-(void)weChatButtonAction:(UIButton *)sender{
    if (self.aliPayBtnClick) {
        self.aliPayBtnClick();
    }
}

//取消点击事件
-(void)alipayButtonAction
{ [UIView animateWithDuration:.15 animations:^{
    self.alpha = 0;
} completion:^(BOOL finished) {
    [self removeFromSuperview];
}];
}

@end
